package FSPackage;

import jade.core.*;
import jade.core.behaviours.*;
import jade.lang.acl.*;
import java.util.*;

class app {
    String name;    // Application name
    int bw;         // required bandwidth

    public app(String appName, int BW) {
        name = appName;
        bw = BW;
    }
}

public class FSAgent extends Agent {
    private int reqBW = 0;
    private Vector<app> apps = new Vector<app>();
    private Random r = new Random();
    int avBW = r.nextInt(300);

    private static final String msgConvID = "DSFSreq";

    public FSAgent() {
        // Application database is made
        // with initial apps
        apps.add(new app("SW1", 10));
        apps.add(new app("SW2", 15));
        apps.add(new app("MSWORD", 35));
        apps.add(new app("NetBeans", 30));
    }

    protected void setup()
    {
        System.err.println("Fileserver started with BW: " + avBW);
        System.err.println("FS: " + getAID().getName());

        addBehaviour(new replyReq(this));
    }

class replyReq extends CyclicBehaviour {

    public replyReq (Agent a) {
        super(a);
    }

    public void action() {

        MessageTemplate mt = MessageTemplate.and(
                MessageTemplate.MatchSender(getAID()),
                MessageTemplate.MatchPerformative(ACLMessage.INFORM));
        ACLMessage req = receive(mt);


        if(req != null) {
            String cont = req.getContent();
            
            for (int i = 0; i < apps.size(); i++) {
                if(cont.equals(apps.elementAt(i).name))
                    reqBW = (avBW < apps.elementAt(i).bw) ?
                        0 : avBW;
            }

            String reply = null;
            reply = "" + reqBW;

            ACLMessage replyMSG = req.createReply();
            replyMSG.setContent(reply);
            //System.err.println("Reply: " + replyMSG.toString());
            send(replyMSG);
        }

        block();
    }
    }
}